-- ------------------------------------------------------------------------
-- > lumSmallTalk (Kreoss @ Quel'Thalas EU) <
-- ------------------------------------------------------------------------
-- Credits: Tekkub, evl, Blooblahguy and zork.
-- ------------------------------------------------------------------------

	local st = CreateFrame("Frame", "lumSmallTalkFrame")
	
-- ------------------------------------------------------------------------
-- > 0. Configuration and Media
-- ------------------------------------------------------------------------

	-- Media
	font = "Interface\\AddOns\\lumSmallTalk\\media\\font.ttf"	
		
	-- Configuration
	fontsize = 12	

-- ------------------------------------------------------------------------
-- > 1. Constants and Variables
-- ------------------------------------------------------------------------
		
	local WhisperColor = {255/255, 0/255, 96/255}
	local BnWhisperColor = {0/255, 120/255, 255/255}

-- ------------------------------------------------------------------------
-- > 2. Functions
-- ------------------------------------------------------------------------

	-- Hide Frame
	local HideFrame = function(f)
	
		f:SetScript('OnShow', f.Hide)
		f:Hide()
	end
	
	-- Update Bottom Button
	local updatebb = function(fr)
	
		local b = fr.buttonFrame.bottomButton
		if fr:AtBottom() then
			b:Hide()
		else
			b:Show()
		end
	end

	-- Bottom Button Click
	local bbClick = function(b)

		local fr = b:GetParent()
		fr:ScrollToBottom()
		updatebb(fr)
	end
	
	-- Alt Click Invite (zork in rChat)
	local origSetItemRef = SetItemRef
	SetItemRef = function(link, text, button)
	  local linkType = string.sub(link, 1, 6)
	  if IsAltKeyDown() and linkType == "player" then
		local name = string.match(link, "player:([^:]+)")
		InviteUnit(name)
		return nil
	  end
	  return origSetItemRef(link,text,button)
	end
	
	-- URL Copy (in rChat by zork)
		
	local foundurl = false
  
	function string.color(text, color)
		return "|cff".."0099FF"..text.."|r"
	end
  
	function string.link(text, type, value, color)
		return "|H"..type..":"..tostring(value).."|h"..tostring(text):color(color or "ffffff").."|h"
	end
  
	local function highlighturl(before,url,after)
		foundurl = true
		return " "..string.link("["..url.."]", "url", url, color).." "
	end
  
	local function searchforurl(frame, text, ...)
  
		foundurl = false
  
		if string.find(text, "%pTInterface%p+") then 
			foundurl = true
		end
		if not foundurl then
			text = string.gsub(text, "(%s?)(%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?:%d%d?%d?%d?%d?)(%s?)", highlighturl)
		end	
		if not foundurl then
			text = string.gsub(text, "(%s?)(%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?)(%s?)", highlighturl)
		end
		if not foundurl then
			text = string.gsub(text, "(%s?)([%w_-]+%.?[%w_-]+%.[%w_-]+:%d%d%d?%d?%d?)(%s?)", highlighturl)
		end
		if not foundurl then
			text = string.gsub(text, "(%s?)(%a+://[%w_/%.%?%%=~&-'%-]+)(%s?)", highlighturl)
		end
		if not foundurl then
			text = string.gsub(text, "(%s?)(www%.[%w_/%.%?%%=~&-'%-]+)(%s?)", highlighturl)
		end
		if not foundurl then
		text = string.gsub(text, "(%s?)([_%w-%.~-]+@[_%w-]+%.[_%w-%.]+)(%s?)", highlighturl)
		end
  
		frame.am(frame,text,...)
 	end
	
	StaticPopupDialogs["TEXT_COPY_POPUP"] = {
  		text = "URL Copy",
		button2 = TEXT(CLOSE),
		hasEditBox = 1,
		hasWideEditBox = 1,
		OnShow = function(self)
			local eB = _G[self:GetName().."EditBox"]
			eB:SetText(curlink)
			curlink = nil
			eB:SetFocus()
			eB:HighlightText(0)
			local bt = _G[self:GetName().."Button2"]
			bt:ClearAllPoints()
			bt:SetWidth(200)
			bt:SetHeight(25)
			bt:SetPoint("CENTER", eB, "CENTER", 0, -30)
		end,
  		timeout = 60,
  		whileDead = true,
		EditBoxOnEscapePressed = function(self) self:GetParent():Hide() end,
  		hideOnEscape = true,
	}
	  
	local orig = ChatFrame_OnHyperlinkShow
	function ChatFrame_OnHyperlinkShow(frame, link, text, button)
		local type, value = link:match("(%a+):(.+)")
		curlink = value
		if ( type == "url" ) then
			StaticPopup_Show("TEXT_COPY_POPUP")		
		else
			orig(self, link, text, button)
		end
	end	
	-- URL Copy -- END
	
	-- Improved Chat Frame
	function st:ImprovedChatFrame()
		
		-- Buttons
		HideFrame(ChatFrameMenuButton)
		HideFrame(FriendsMicroButton)
		HideFrame(ChatFrameMenuButton)
		HideFrame(FriendsMicroButton)
		
		--Tabs
		CHAT_FRAME_FADE_OUT_TIME = 1
		CHAT_TAB_HIDE_DELAY = 1
		CHAT_FRAME_TAB_SELECTED_MOUSEOVER_ALPHA = 1
		CHAT_FRAME_TAB_SELECTED_NOMOUSE_ALPHA = 0
		CHAT_FRAME_TAB_NORMAL_MOUSEOVER_ALPHA = 1
		CHAT_FRAME_TAB_NORMAL_NOMOUSE_ALPHA = 0
		CHAT_FRAME_TAB_ALERTING_MOUSEOVER_ALPHA = 1
		CHAT_FRAME_TAB_ALERTING_NOMOUSE_ALPHA = 0
		
		-- Move Battle.net Toast
		BNToastFrame:SetClampedToScreen(true)
					
		for t = 1, NUM_CHAT_WINDOWS do
		
			local fr = "ChatFrame" .. t
			local cf = _G[fr] -- ChatFrame
			local eb = _G[fr .. "EditBox"] -- Editbox
			local ebtex = ({_G["ChatFrame".. t .."EditBox"]:GetRegions()})
			
			-- Chat Copy
			
			if ( t ~= 2 ) then
		  		cf.am = cf.AddMessage
		  		cf.AddMessage = searchforurl
			end
					
			-- EditBox		
			eb:ClearAllPoints()			
            eb:SetPoint("BOTTOMLEFT", ChatFrame1Tab, "TOPLEFT", -2, -10)
            eb:SetPoint("BOTTOMRIGHT", cf, "TOPRIGHT", 2, 0)
			ebtex[6]:SetAlpha(0) ebtex[7]:SetAlpha(0) ebtex[8]:SetAlpha(0) ebtex[9]:SetAlpha(0) ebtex[10]:SetAlpha(0) ebtex[11]:SetAlpha(0)
			eb:SetBackdrop({bgFile = 'Interface\\Buttons\\WHITE8x8', edgeFile = 'Interface\\Buttons\\WHITE8x8', edgeSize = 2, insets = {top = 2, left = 2, bottom = 2, right = 2}})
    		eb:SetBackdropColor(0,0,0,.7)
    		eb:SetBackdropBorderColor(0,0,0,1)
			eb:SetHeight(25)
			
			eb:HookScript('OnEditFocusGained', function(s) s:Show() end)
			eb:HookScript('OnEditFocusLost', function(s) s:Hide() end)
			
			-- Tabs
			_G["ChatFrame".. t .."TabLeft"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabMiddle"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabRight"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabHighlightLeft"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabHighlightRight"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabHighlightMiddle"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabSelectedMiddle"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabSelectedRight"]:SetTexture(nil)
			_G["ChatFrame".. t .."TabSelectedLeft"]:SetTexture(nil)
						
			-- Resizing and Position
			cf:SetMinResize(0,0)
			cf:SetMaxResize(0,0)
			cf:SetClampedToScreen(false)

			_G["ChatFrame".. t .."ResizeButton"]:SetScale(.5)
    		_G["ChatFrame".. t .."ResizeButton"]:SetAlpha(0.5)			
			
			-- Scrolling Edit Text without Alt key 
			eb:SetAltArrowKeyMode(false)
			
			-- Font
			cf:SetFont(font, fontsize, "THINOUTLINE")
				
			-- Disable Fading
			cf:SetFading(nil)
			
			-- Buttons
			cf:HookScript("OnShow", updatebb)
			cf.buttonFrame:Hide()
			local bb = cf.buttonFrame.bottomButton
			bb:SetParent(fr)
			bb:SetPoint("BOTTOMRIGHT", cf, "BOTTOMRIGHT")
			bb:SetScript("OnClick", bbClick)
			bb:SetAlpha(0.8)
			updatebb(cf)			
		end
		
		hooksecurefunc("FloatingChatFrame_OnMouseScroll", updatebb)
		
		-- Make Channels Sticky
		ChatTypeInfo.CHANNEL.sticky = 1	
	end
	
	
	function st:PLAYER_ENTERING_WORLD(self,event)
		
		ChangeChatColor("BN_WHISPER", unpack(BnWhisperColor))
		ChangeChatColor("WHISPER", unpack(WhisperColor))
		st:UnregisterEvent('PLAYER_ENTERING_WORLD')
	end
	
	function st:ADDON_LOADED(self,event,...)

		local addon_name = ...
		
		if addon_name == "lumSmallTalk" then	
			-- Events
			st:RegisterEvent("PLAYER_ENTERING_WORLD")
		end
	end
	
	function st:OnEvent(self,event,...)
	
		local addon_name = ...
	
		if ((event == "ADDON_LOADED") and (addon_name == "lumSmallTalk")) then self:ADDON_LOADED(self,event,...)
		elseif (event == "PLAYER_ENTERING_WORLD") then self:PLAYER_ENTERING_WORLD(self,event,...)
		end	
	end
	
	--------------
	-- Main Calls
	--------------
	
	st:ImprovedChatFrame()
	
-- ------------------------------------------------------------------------
-- > 3. Events
-- ------------------------------------------------------------------------

	st:SetScript("OnEvent", function(self,event,...) self:OnEvent(self,event,...) end)
	st:RegisterEvent("ADDON_LOADED")